package com.zhongxi.guoyuan.view.personalCenter;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.PermissionChecker;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.ApplyRecordAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.user.ApplyRecordResult;
import com.zhongxi.guoyuan.url.Url;

import org.json.JSONException;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WithDrawCashActivity extends BaseActivity {


    @Bind(R.id.ivBack)
    ImageView ivBack;
    @Bind(R.id.tvTitle)
    TextView tvTitle;
    @Bind(R.id.lvList)
    ListView lvList;
    @Bind(R.id.withdraw_tv_shopname)
    TextView withdrawTvShopname;
    @Bind(R.id.withdraw_tv_shopphone)
    TextView withdrawTvShopphone;
    @Bind(R.id.withdraw_tv_call)
    TextView withdrawTvCall;
    @Bind(R.id.withdraw_tv_address)
    TextView withdrawTvAddress;

    private ApplyRecordResult result;
    private ArrayList<ApplyRecordResult.ApplyRecordData> data = new ArrayList<>();
    private ApplyRecordAdapter adapter;

    private final int REQUEST_CODE=1000;
    private String phone;


    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_with_draw_cash);
        initTitle();
        ButterKnife.bind(this);
        tvTitle.setText("提现助手");

    }

    @Override
    protected void initEvent() {
        super.initEvent();

    }

    @Override
    protected void initData() {
        super.initData();
        getRecorder();
    }


    /**
     * 获取提现记录
     */
    private void getRecorder() {

        getServer(Url.APPLY_RECORD, null, "", false, null, "get");
    }

    @Override
    protected void processSuccessResult(String response) throws JSONException {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try {
            result = gson.fromJson(response, ApplyRecordResult.class);
            MyApplication.getInstance().setSpValue(result.getSafeobj());

            data.clear();

            if (null != result.getInfo() && result.getInfo().size() > 0) {
                data.addAll(result.getInfo());
            }

            adapter = new ApplyRecordAdapter(context, data);
            lvList.setAdapter(adapter);

            initinfo(result.getStore());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initinfo(ApplyRecordResult.Store store) {
        if (null!=store){
           if (null!=store.getName()&&!"".equals(store.getName())){
               withdrawTvShopname.setText(store.getName());
           }else {
               withdrawTvShopname.setText("");
           }
           if (null!=store.getMphone()&&!"".equals(store.getMphone())){
               withdrawTvShopphone.setText(store.getMphone());
           }else {
               withdrawTvShopphone.setText("");
           }
           if (null!=store.getAddress()&&!"".equals(store.getAddress())){
               withdrawTvAddress.setText(store.getAddress());
           }else {
               withdrawTvAddress.setText("");
           }
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try {
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if (null != result.getSafeobj() && !"".equals(result.getSafeobj())) {
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @OnClick({R.id.ivBack, R.id.withdraw_tv_call})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ivBack:
                finish();
                break;
            case R.id.withdraw_tv_call://请求拨打电话

                phone = withdrawTvShopphone.getText().toString().trim();
                if (!"".equals(phone)){
                    requestCallphone(phone);
                }else {
                    showShortToast("号码不能为空");
                }

                break;
        }
    }

    //获取权限
    private void requestCallphone(String phone) {
        if (Build.VERSION.SDK_INT>=23){
            //判断有没有拨打权限
            if (PermissionChecker.checkSelfPermission(WithDrawCashActivity.this, Manifest.permission.CALL_PHONE)!= PackageManager.PERMISSION_GRANTED){
                ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CODE);
            }else {
                callPhone(phone);
            }
        }else {
            callPhone(phone);
        }
    }

    private void callPhone(String phone){
        Uri uri = Uri.parse("tel:" + phone);
        Intent intent = new Intent(Intent.ACTION_DIAL, uri);
        //此处不判断就会报错
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
            startActivity(intent);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode==REQUEST_CODE&&PermissionChecker.checkSelfPermission(this,Manifest.permission.CALL_PHONE)==PackageManager.PERMISSION_GRANTED){
            showShortToast("授权成功");
            callPhone(phone);
        }
    }


}
